<?php
namespace App\Services;

use App\Models\NewsModel;
use App\Models\ServoModel;
use App\Models\StoreModel;
use App\Models\TagsModel;
use App\Models\UserModel;
use Barryvdh\Reflection\DocBlock\Tag;
use Framework\BaseService;

class IndexService extends BaseService
{


    public function getTags($type)
    {
        if(!$type){
            $this->retError("缺少参数");
        }
        $data = TagsModel::query()->where('type',$type)->pluck('name');
        return $data;
    }

    public function index()
    {
        //资讯
        /*$news = app(NewsService::class)
            ->newsList($cate_id = 0, $is_elite = 1, $size = 2, $page = 1);*/
        $news = NewsModel::query()->where(['status'=>1])
            ->select('id','title','share_times','created_at','head_img','like_times','tags','view_times')
            ->orderBy('is_elite','desc')
            ->forpage(1,2)
            ->get()
            ->map(function ($item){
                if(strstr($item->head_img, 'https://') == false){
                    $item->head_img = 'https://'.config('filesystems.disks.qiniu.domains.default').$item->head_img.'?imageslim';
                }
                return $item;
            });
        //推荐门店
        $store = StoreModel::query()
            ->where(['status'=>1])
            ->select('id','store_images','lng_point','lat_point','service_star','store_name','service_num')
            ->orderBy('sort','desc')
            ->forpage(1,2)
            ->get();
        $sub_user = UserModel::query()->select(['id AS user_id', 'avatar as user_avatar']);
        $service_user_qt = ServoModel::query()
//            ->with('servo_re')
            ->leftJoinSub($sub_user,'user_info', 'servo.user_id', '=', 'user_info.user_id')
            ->select('servo.servo_name','servo.tags','servo.honorary_title','user_info.user_avatar','servo.star_level','servo.id')
            ->where(['status'=>1,'type'=>1])->forpage(1,3)->get();

        $service_user_zd = ServoModel::query()
//            ->with('servo_re')
            ->leftJoinSub($sub_user,'user_info', 'servo.user_id', '=', 'user_info.user_id')
            ->select('servo.servo_name','servo.tags','servo.honorary_title','user_info.user_avatar','servo.star_level','servo.id')
            ->where(['status'=>1,'type'=>3])->forpage(1,4)->get();

        $service_user_dd = ServoModel::query()
//            ->with('servo_re')
            ->leftJoinSub($sub_user,'user_info', 'servo.user_id', '=', 'user_info.user_id')
            ->select('servo.servo_name','servo.tags','servo.honorary_title','user_info.user_avatar','servo.star_level','servo.id')
            ->where(['status'=>1,'type'=>4])->forpage(1,10)->get();
        $data['news'] = $news;
        $data['store'] = $store;
        $data['service_user_qt'] = $service_user_qt;
        $data['service_user_zd'] = $service_user_zd;
        $data['service_user_dd'] = $service_user_dd;
        return $data;
    }



}
